package server_thread;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class FileUploadServerThread implements Runnable{
	
private Socket socket;
	
	public FileUploadServerThread(Socket socket){
		this.socket = socket;
	}

	public void run() {
		OutputStream out;
		try {
			out = socket.getOutputStream();
			BufferedReader bReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String filename = bReader.readLine();
			OutputStream outputStream = new FileOutputStream("scp/"+filename);
			byte[] buf = new byte[20];
			int len =0;
			int count =0;
			System.out.println(filename);
			int ava =Integer.valueOf(bReader.readLine()) ;
			System.out.println(ava);
			while ((len=socket.getInputStream().read(buf))!=-1) {
				outputStream.write(buf, 0, len);
				count+=len;
				System.out.println("count:"+count);
				if (count==ava) {
					break;
				}
			}
			outputStream.close();
			System.out.println("hehe");
			out.write("upload success\n".getBytes());
			out.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
